import { getInfoById, getInfoProfile } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
      setToken(token)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    getUser(state, info) {
      state.userInfo = info
    },
    delUser(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async getUserInfo(context) {
      const res = await getInfoProfile()
      console.log(res, 'getInfoProfile')

      const info = await getInfoById(res.data.userId)
      console.log(info)
      context.commit('getUser', { ...res.data, ...info.data })
      return res.data.roles.menus
    },
    logout(context) {
      context.commit('delToken')
      context.commit('delUser')
      resetRouter()
    }
  }
}
